home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Apple Developer Connection Student Program
/
ADC Tools Sampler CD Disk 3 1999.iso
/
Documentation
/
Books
/
Learn Java on the Macintosh
/
Learn Java Projects
/
13.01 - applet params
/
SimpleDraw.java
< prev
next >
Wrap
Text File
|
1996-04-22
|
4KB
|
132 lines
/* -------------------------------------------------------------
This applet paints a circle or square of the color you've chosen
wherever you click. This applet keeps a list of the shapes you've drawn
and paints all the shapes in the list when it repaints. It allows the
HTML file to supply a list of colors for the shapes.
Java's classes: Applet (applet)
Event (awt) user-generated action
Graphics (awt) used for drawing
Color (awt) defines colors
Choice (awt) shape and color selection choices
Vector (util) list of shapes
Custom classes: SimpleDraw
Circle defines and draws circles
Square defines and draws squares
Shape a common ancestor for circles and squares
------------------------------------------------------------- */
import java.applet.Applet;
import java.util.*;
import java.awt.*;
public class SimpleDraw extends Applet {
Vector drawnShapes;
Choice shapeChoice;
Choice colorChoice;
/** Create the GUI. */
public void init() {
drawnShapes = new Vector();
shapeChoice = new Choice();
shapeChoice.addItem("Circle");
shapeChoice.addItem("Square");
add(shapeChoice);
colorChoice = new Choice();
colorChoice.addItem(getParameter("color1")); // supplied by applet
colorChoice.addItem(getParameter("color2")); // supplied by applet
colorChoice.addItem(getParameter("color3")); // supplied by applet
add(colorChoice);
}
/** Draw all the shapes. */
public void paint(Graphics g) {
Shape s;
int numShapes;
numShapes = drawnShapes.size();
for (int i = 0; i < numShapes; i++) {
s = (Shape)drawnShapes.elementAt(i);
// When the shape draws, circles and squares each invoke their own
// draw method, depending on which shape this is.
s.draw(g);
}
}
/** Create a new shape. */
public boolean mouseUp(Event e, int x, int y) {
Shape s; // This shape will be either a circle or a square.
String shapeString = shapeChoice.getSelectedItem();
String colorString = colorChoice.getSelectedItem();
if (shapeString.equals("Circle"))
s = new Circle();
else
s = new Square();
if (colorString.equals("Red"))
s.color = Color.red;
else if (colorString.equals("Green"))
s.color = Color.green;
else if (colorString.equals("Black"))
s.color = Color.black;
else if (colorString.equals("Blue"))
s.color = Color.blue;
else if (colorString.equals("Pink"))
s.color = Color.pink;
else if (colorString.equals("Cyan"))
s.color = Color.cyan;
else if (colorString.equals("Orange"))
s.color = Color.orange;
else
s.color = Color.white; // default color
s.x = x;
s.y = y;
drawnShapes.addElement(s);
repaint();
return true;
}
}
/** Shapes provide common characteristics for the circle and square. */
abstract class Shape {
static public final int shapeRadius = 20;
Color color;
int x;
int y;
abstract void draw(Graphics g);
}
/** Draws and maintains circle information. */
class Circle extends Shape {
void draw(Graphics g) {
g.setColor(this.color);
g.fillOval(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);
}
}
/** Draws and maintains square information. */
class Square extends Shape{
void draw(Graphics g) {
g.setColor(this.color);
g.fillRect(this.x - shapeRadius, this.y - shapeRadius, shapeRadius * 2, shapeRadius * 2);
}
}